package com.itboye.mengwu.app.ui.aiwujia;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.RedBagModel;
import com.itboye.mengwu.data.presenter.RedBagPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class YouHuiQuanYiShiYong extends BaseFragment implements Observer {
    ListView old_red_bag_list;
    MyAdapter adapter;
    int status;
    ArrayList<RedBagModel> arrayList = new ArrayList<RedBagModel>();
    private RedBagPresenter bagPresenter;
    TextView nodata;

    public YouHuiQuanYiShiYong(int status) {
        this.status = status;
    }

    @Override
    public int initView() {
        // TODO Auto-generated method stub
        return R.layout.list_red_bag;
    }

    @Override
    public void onMyActivityCreated() {
        bagPresenter = new RedBagPresenter(this);
        RedBagModel model = new RedBagModel();
        model.setUid((String) SPUtils.get(getActivity(), null,
                SpPublicParams.UID, ""));
        model.setUse_status(status + "");
        bagPresenter.queryYouHuiQuan(model);
    }

    class MyAdapter extends BaseAdapter {
        Context context;
        ArrayList<RedBagModel> arrayList;

        public MyAdapter(Context context, ArrayList<RedBagModel> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(context);

                convertView = inflater.inflate(R.layout.item_red_bag_used, null);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.xianzhi = (TextView) convertView
                        .findViewById(R.id.xianzhi);
                holder.price = (TextView) convertView.findViewById(R.id.price);
                holder.use_time = (TextView) convertView
                        .findViewById(R.id.use_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.name.setText(arrayList.get(position).getName());
            holder.xianzhi.setText(arrayList.get(position).getNotes());
            holder.price.setText(arrayList.get(position).getMoney());
            System.out.println(arrayList.get(position).getExpire_time());
            System.out.println(arrayList.get(position).getExpire() + "转换后" + Long.parseLong(arrayList.get(position).getExpire()));
            holder.use_time.setText((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.parseLong(arrayList.get(position).getExpire()) * 1000)));
            return convertView;
        }

        public class ViewHolder {
            TextView name, xianzhi, price, use_time;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub

        old_red_bag_list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                if (arg2 == adapter.getCount() - 1) {
                    Log.d("hu", "linghongbao");
                }

            }
        });
        ResultEntity entity = handlerError(data);
        arrayList = (ArrayList<RedBagModel>) entity.getData();
        if (arrayList != null) {
            if (arrayList.size() > 0) {
                nodata.setVisibility(View.GONE);
                adapter = new MyAdapter(getActivity(), arrayList);
                old_red_bag_list.setAdapter(adapter);
            } else {
                nodata.setVisibility(View.VISIBLE);
            }

        } else {
            nodata.setVisibility(View.VISIBLE);
        }

    }

}
